package cn.jdy.plugins.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Business;
import cn.jdy.plugins.entity.Collect;
import cn.jdy.plugins.entity.CollectPK;

/**
 * @title : 我的收藏实现类
 * @description :
 */
@Service
public class CollectService extends BaseDao<Collect> {

	public Map<String, Object> query(Collect collect, String userId, String fromDate, String toDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuilder sqlQuery = new StringBuilder();
		StringBuilder sqlCount = new StringBuilder();
		StringBuilder sqlWhere = new StringBuilder(" from collect where userId = ?");
		if(fromDate!=null && fromDate.length()>0 && toDate!=null && toDate.length()>0) {
			sqlWhere.append(" and createTime between ").append(fromDate).append(" and ").append(toDate);
		} else if(fromDate!=null && fromDate.length()>0) {
			sqlWhere.append(" and createTime > ").append(fromDate);
		} else if(toDate!=null && toDate.length()>0) {
			sqlWhere.append(" and createTime < ").append(toDate);
		}
		sqlCount.append("select count(1)").append(sqlWhere);

		sqlWhere.append(" order by createTime DESC");
		sqlQuery.append("select userId, businessId, createTime").append(sqlWhere);

		SQLQuery query = super.em().createSQLQuery(sqlQuery.toString());
		query.setParameter(0, userId);
		query.setFirstResult(collect.getiDisplayStart());
		query.setMaxResults(collect.getiDisplayLength());
		query.addEntity(Collect.class);
	
		SQLQuery sqlcount = super.em().createSQLQuery(sqlCount.toString());
		sqlcount.setParameter(0, userId);
		
		Object obj = sqlcount.uniqueResult();
		long count = 0l;
		if(obj!=null){
			count = Long.parseLong(obj.toString());
		}

		map.put("dataList", query.list());
		map.put("total", count);
		
		return map;
	}

	/**
	 * 商机是否被收藏
	 * @param pk
	 * @param bus
	 */
	public List<Business> isCollect(List<Business> list, String userId) {
	  // select 
		if(list!=null && list.size()>0) {
			CollectPK pk = new CollectPK(userId, null);
			for(Business b : list) {
				pk.setBusinessId(b.getId());
				isBusCollect(pk,b);
			}
		}
		return list;
	}
	
	/**
	 * 商机是否被收藏
	 * @param pk
	 * @param bus
	 */
	public void isBusCollect(CollectPK pk,Business bus){
		bus.setCollect(super.find(pk)!=null);
	}

	/**
	 * 查看收藏次数
	 * @return
	*/
    public Long count(String userId,String businessId) {
	    String sql = "Select count(1) from collect where 1=1 ";
	    if(!StringUtil.isEmpty(userId))
	    	sql += " and userId = '"+userId+"'";
	    if(!StringUtil.isEmpty(businessId))
	    	sql += " and businessId = '"+businessId+"'";
	    
    	SQLQuery queryCount = super.em().createSQLQuery(sql);
		Object obj = queryCount.uniqueResult();
		if(obj!=null){
			return Long.parseLong(obj.toString());
		}else{
			return 0l ;
		}
	}
    
    
    /**
	 * 删除ID
	 * @param pk
	 */
	public void delete(CollectPK pk){
		String sql = "delete from collect where 1=1 ";
	    if(!StringUtil.isEmpty(pk.getUserId()))
	    	sql += " and userId = '"+pk.getUserId()+"'";
	    if(!StringUtil.isEmpty(pk.getBusinessId()))
	    	sql += " and businessId = '"+pk.getBusinessId()+"'";
    	SQLQuery query = super.em().createSQLQuery(sql);
    	query.executeUpdate();
	}
	
	

}
